package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.*;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.picc.entity.ActivityEnum;
import me.luger.project.picc.entity.picc.PolicyResult;
import me.luger.project.picc.service.picc.PiccService;
import me.luger.project.picc.service.picc.TongjiService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.PiccMeetingService;
import me.luger.project.picc.entity.PiccMeeting;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 我和保单有个约会 controller
 */
@Controller
@RequestMapping("/api/picc/piccMeeting")
public class PiccMeetingApiController extends BaseController {

    private static final String PIC_PATH = "/usr/local/nginx/html/piccImg/";
    private static final String PIC_URL_PREFIX = "/piccImg/";



    @Autowired
    private PiccMeetingService piccMeetingService;

    @Autowired
    private PiccService piccService;

    @Autowired
    private TongjiService tongjiService;

    /**
     * String openId,
     String pid,
     String userName,
     String phoneNum,
     String cid,
     String message,
     String address,
     String zCode,
     String imgBase64Str
     * @param imgBase64Str
     * @return
     */
    @RequestMapping(value = "/save",name = "保存我和保单有个约会接口")
    @ResponseBody
    public Object save(PiccMeeting piccMeeting,
                       String imgBase64Str){
        int bizId = 1;
        try{
            String pid = piccMeeting.getPid();
            String openId = piccMeeting.getOpenId();
            String userName = piccMeeting.getUserName();
            PolicyResult policyResult = piccService.checkPolicyResult(openId,userName,pid);
//            if(null != piccMeetingService.fetchByPid(pid)){
//                return Result.info(-1,"该保单号参加过该活动");
//            }

            boolean exist = policyResult.isExist();
            boolean normal = policyResult.isNormal();
            boolean dateRight = policyResult.isDateRight();
            boolean overOneYear = policyResult.isOverOneYear();
            boolean newRet = policyResult.isNewRet();
            boolean self = policyResult.isSelf();
            boolean periodPay = policyResult.isPeriodPay();

            if(!exist){
                return Result.info(-2,"您填写的保单不在本活动范围之内，请重新填写！");
            }

            if(!normal){
                return Result.info(-2,"您的保单已经超过有效期，请重新填写");
            }

            if(!self){
                return Result.info(-2,"您填写的投保人与保单信息不一致，请重新填写");
            }

            String picName = pid + "_" + System.currentTimeMillis() + ".jpg";
            ImgUtil.GenerateImage(imgBase64Str,PIC_PATH+picName);
            piccMeeting.setPicPath(PIC_URL_PREFIX+picName);


            if(null != policyResult && policyResult.isExist() && policyResult.isSelf()){
                piccMeeting.setDoPrizeStatus(1);
            }else{
                piccMeeting.setDoPrizeStatus(0);
            }

            piccMeeting = piccMeetingService.insert(piccMeeting);
            bizId = piccMeeting.getId();
            tongjiService.addUserCount(piccMeeting.getOpenId(), ActivityEnum.PICC_MEETING,0);



            piccService.sendPiccMetting(piccMeeting);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("bizId",bizId);
    }

    @RequestMapping(value = "/doDraw",name = "领导抽奖仪式接口")
    @ResponseBody
    public Object doDraw(int rank,
                         int count){
        List<PiccMeeting> piccMeetingList = null;
        try{
            piccMeetingList = piccMeetingService.doDraw(rank,count);
            if(null == piccMeetingList){
                return Result.info(-1,"没有符合条件的中奖者了");
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("userList",piccMeetingList);
    }

    @RequestMapping(value = "/export2Excel",name = "导出抽奖数据")
    @ResponseBody
    public Object export2Excel(){
        return Result.success().addDateItem("filePath",piccMeetingService.export2Excel());
    }

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
